<?php

    class CartController extends Zend_Controller_Action
    {

        public function init()
        {
            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();
            if (!empty($y))
            {
                $this->user_id = $x->read()->rkn_uid;
            }
            else
            {
                echo 'Please login';
                die;
            }
            $this->user = (array) $y;
        }

        public function indexAction()
        {
            if (isset($this->user_id))
            {
                $user_id = $this->user_id;
                $model = new Application_Model_Common();
                $alldata = $model->getAdapter()->select()->from(array("c" => RKN_CART), array('cart_id', 'product_color', 'product_size', 'user_id', 'product_id', 'brand_id', 'product_quantity', new Zend_Db_Expr('(select sum(' . RKN_PRODUCT . '.price*product_quantity) from ' . RKN_CART . ' inner join ' . RKN_PRODUCT . ' on ' . RKN_PRODUCT . '.rkn_prod_id = ' . RKN_CART . '.product_id  where ' . RKN_CART . '.user_id = "' . $user_id . '" ) as total_price')))
                        ->join(array("p" => RKN_PRODUCT), "c.product_id=p.rkn_prod_id")
                        ->join(array("b" => RKN_BRAND), "p.brand_id=b.brand_id")
                        ->where("c.user_id = $user_id ")
                        ->group("c.cart_id")
                        ->order("c.cart_id DESC");

                //prd($alldata->__toString());

                $alldata = $alldata->query()->fetchAll();

                //prd($alldata);
                //random products
                $randomProducts = $model->getAdapter()->select()->from(RKN_PRODUCT)
                        ->joinLeft(RKN_SUB_CHILD_CATEGORY, RKN_PRODUCT . ".schild_id = " . RKN_SUB_CHILD_CATEGORY . ".schild_id")
                        ->order(" rand() ")
                        ->limit(" 4 ");

                $randomProducts = $randomProducts->query()->fetchAll();

                $this->view->randomProducts = $randomProducts;

                $subtotal_price = 0;
                foreach ($alldata as $key => $value)
                {
                    $subtotal_price = $subtotal_price + ($value["price"] * $value["product_quantity"]);
                }
                //prd($alldata);
                $this->view->cartdata = (!empty($alldata) && $alldata[0]['cart_id'] ) ? $alldata : '';

                $shippingPlugin = new Application_Plugin_ShippingCalculator();
                $this->view->shippingPlugin = array(
                    '01' => 'UPS Next Day Air',
                    '02' => 'UPS 2nd Day Air',
                    '03' => 'UPS Ground',
                    '07' => 'UPS Worldwide Express',
                    '08' => 'UPS Worldwide Expedited',
                    '11' => 'UPS Standard',
                    '12' => 'UPS 3 Day Select',
                    '13' => 'UPS Next Day Air Saver',
                    '14' => 'UPS Next Day Air Early A.M.',
//                    '54' => 'UPS Worldwide Express Plus',
//                    '59' => 'UPS 2nd Day Air A.M.',
//                    '65' => 'UPS Saver',
//                    ## 82-86 are Polish Domestic Shipments
//                    '82' => 'UPS Today Standard',
//                    '83' => 'UPS Today Dedicated Courier',
//                    '84' => 'UPS Today Intercity',
//                    '85' => 'UPS Today Express',
//                    '86' => 'UPS Today Express Saver'
                );
                $shipping_code = "01";
                $this->view->preSelectShipping = $shipping_code;

                $this->view->subtotal = round($subtotal_price, 2);
                $this->view->shipping = $this->getShippingCharge($shipping_code);
                $this->view->promo_discount = 0;
                $this->view->tax = calculateTax($subtotal_price);
                $this->view->user = $this->user;
            }
        }

        public function calculationAction()
        {
            if ($this->getRequest()->isPost())
            {
                $arr = $this->getRequest()->getPost();
//                prd($arr);
                $subtotal = $arr["subtotal"];

                $promo_discount = 0;
                if (isset($arr["promo_discount"]) || !empty($arr["promo_discount"]))
                {
                    $promo_discount = $arr["promo_discount"];
                }

                $shipping = 0;
                if (isset($arr["shipping"]) || !empty($arr["shipping"]))
                {
                    $shipping = $arr["shipping"];
                }

                if (isset($arr["shipping_code"]) || !empty($arr["shipping_code"]))
                {
                    $shipping_code = $arr["shipping_code"];
                    $shipping = $this->getShippingCharge($shipping_code);
                    if (empty($shipping))
                        $shipping = 0;
                }

                $total = $grand_total = round(($subtotal - $promo_discount), 2);
                $tax = calculateTax($total);
                $grand_total = round(($subtotal - $promo_discount + $shipping + $tax), 2);

                $array = array(
                    "subtotal" => $subtotal,
                    "promo_discount" => $promo_discount,
                    "shipping" => $this->getShippingCharge($shipping_code),
                    "tax" => $tax,
                    "grand_total" => $grand_total
                );

                echo json_encode($array);
            }
            die;
        }

        public function updatecartajaxAction()
        {
            if ($this->getRequest()->isPost())
            {
                $data_array = array();
                $arr = $this->getRequest()->getPost();
                $model = new Application_Model_Common();
                $cart_id = $arr["cart_id"];
                $shipping_code = $arr["shipping_code"];

                if (isset($arr["quantity"]))
                {
                    $data_array["product_quantity"] = $arr["quantity"];
                }

                if (isset($arr["color"]))
                {
                    $data_array["product_color"] = $arr["color"];
                }

                if (isset($arr["size"]))
                {
                    $data_array["product_size"] = $arr["size"];
                }
                $model->updateRecordData($data_array, "cart_id=$cart_id", RKN_CART);


                // re-calculate subtotal

                $user_id = $this->user_id;
                $sql = "SELECT price, product_quantity FROM " . RKN_CART . " as c INNER JOIN " . RKN_PRODUCT . " as p ON p.rkn_prod_id = c.product_id WHERE c.user_id = $user_id";
                $result = $model->getAdapter()->query($sql)->fetchAll();
//                prd($result);

                $subtotal = 0;
                foreach ($result as $rKey => $rValue)
                {
                    $quantity = $rValue["product_quantity"];
                    $price = $rValue["price"];
                    $subtotal = $subtotal + ($price * $quantity);
                }

                $array = array(
                    "producttotal" => $arr["quantity"] * $arr["product_price"],
                    "subtotal" => $subtotal,
                    "tax" => calculateTax($subtotal),
                    "shipping" => $this->getShippingCharge($shipping_code)
                );

                echo json_encode($array);
            }
            die;
        }

        public function checkoutpaypalAction()
        {
            if (isset($this->user_id) && !empty($this->user_id))
            {
                $paypal_email = PAYPAL_EMAIL;
                $return_url = APPLICATION_URL . "/cart/paymentsuccess";
                $cancel_url = APPLICATION_URL . "/cart/paymentcancel";
                $form_action = PAYPAL_FORM_URL;

                $user_id = $this->user_id;
//              $shipping_cost = $this->_getParam("shipping_cost");
                $shipping_code = $this->_getParam("shipping_code");
//                $promo_code = $arr["promo_code"];

                $model = new Application_Model_Common();

                $sql = "SELECT prod_name,price, product_quantity FROM " . RKN_CART . " as c INNER JOIN " . RKN_PRODUCT . " as p ON p.rkn_prod_id = c.product_id WHERE c.user_id = $user_id";
                $result = $model->getAdapter()->query($sql)->fetchAll();

                if (!empty($result))
                {
                    $k = 1;

                    echo "<h3>You are being redirected. Please wait...</h3>";
                    echo '<form action="' . $form_action . '" name="payment_frm" id="payment_frm" method="post">';
                    echo '<input type="hidden" name="cmd" value="_cart">';
                    echo '<input type="hidden" name="upload" value="1">';
                    echo '<input type="hidden" name="custom" value="">';
                    echo '<input type="hidden" name="business" value="' . $paypal_email . '">';

                    foreach ($result as $key => $value)
                    {
                        $product_name = $value["prod_name"];
                        $price = $value["price"];
                        $product_quantity = $value["product_quantity"];

                        echo '<input type="hidden" name="item_name_' . $k . '" value="' . $product_name . '">';
                        echo '<input type="hidden" name="amount_' . $k . '" value="' . $price . '">';
                        echo '<input type="hidden" name="quantity_' . $k . '" value="' . $product_quantity . '">';
                        echo '<input type="hidden" name="tax_' . $k . '" value="' . calculateTax($price) . '">';
//                        echo '<input type="hidden" name="shipping_' . $k . '" value="' . $shipping_cost . '">';
                        if (!empty($shipping_code))
                            echo '<input type="hidden" name="shipping_' . $k . '" value="' . $this->getShippingCharge($shipping_code, "5", $product_quantity) . '">';

                        $k++;
                    }

//                    echo '<input type="hidden" name="shipping" value="' . $shipping_cost . '">';
//                    echo '<input type="hidden" name="shipping2" value="' . $shipping_cost . '">';

                    echo '<input type="hidden" name="currency_code" value="USD">';
                    echo '<input type="hidden" name="charset" value="utf-8">';
                    echo '<input type="hidden" name="rm" value="2" >';
                    echo '<input type="hidden" name="return" id="return" value="' . $return_url . '"/>';
                    echo '<input type="hidden" name="cancel_return" value="' . $cancel_url . '">';
                    echo '<input type="submit" value="Paypal" name="btn" style="display:none;" />';
                    echo '</form>';
                    echo '<script>window.document.payment_frm.submit();</script>';
                }
                else
                {
                    echo 'Your cart is empty';
                }
            }
            die;
        }

        public function removeAction()
        {
            if ($this->getRequest()->isPost() && isset($this->user_id))
            {
                $user_id = $this->user_id;
                $arr = $this->getRequest()->getPost();
                $cart_id = $arr["cart_id"];
                $shipping_code = $arr["shipping_code"];
                $model = new Application_Model_Common();
                $model->deleteRecord("cart_id = $cart_id AND user_id = $user_id", RKN_CART);

                $tmpsql = "SELECT COUNT(cart_id) as totalcount FROM " . RKN_CART . " WHERE user_id = $user_id";
                $tmpresult = $model->getAdapter()->query($tmpsql)->fetch();

                $sql = "SELECT price, product_quantity FROM " . RKN_CART . " as c INNER JOIN " . RKN_PRODUCT . " as p ON p.rkn_prod_id = c.product_id WHERE c.user_id = $user_id";
                $result = $model->getAdapter()->query($sql)->fetchAll();
//                prd($result);

                $subtotal = 0;
                foreach ($result as $rKey => $rValue)
                {
                    $quantity = $rValue["product_quantity"];
                    $price = $rValue["price"];
                    $subtotal = $subtotal + ($price * $quantity);
                }

                $shipping_charge = "";
                if (!empty($shipping_code))
                    $shipping_charge = $this->getShippingCharge($shipping_code);

                $array = array(
                    "response" => "1",
                    "cartItems" => $tmpresult["totalcount"],
                    "subtotal" => $subtotal,
                    "tax" => calculateTax($subtotal),
                    "shipping" => $shipping_charge
                );
                echo json_encode($array);
            }
            die;
        }

        public function paymentsuccessAction()
        {
            $arr = $this->getRequest()->getPost();

            //prd($arr);
            if ($this->getRequest()->isPost())
            {
                $user_id = $this->user_id;
                $model = new Application_Model_Common();
                $email_model = new Application_Model_Email();
//                prd($arr);
//                $cart_products = $model->getAllRecordDetail(RKN_CART, "user_id=$user_id");
                $cart_products = $model->getAllRecordFromInnerJoin(array("c" => RKN_CART), array("p" => RKN_PRODUCT), "p.rkn_prod_id=c.product_id", "user_id=$user_id");

                $productArr = array();
                $quantityArr = array();
                
                foreach ($cart_products as $cartKey => $cartVal)
                {

                    $productArr[] = $cartVal['product_id'];
                    $sizeArr[] = $cartVal['product_size'];
                    $colorArr[] = $cartVal['product_color'];
                    $quantityArr[] = $cartVal['product_quantity'];
                }
                
                
                
                $productArr = implode('|||', $productArr);
                $quantityArr = implode('|||', $quantityArr);
                $sizeArr = implode('|||',$sizeArr);
                $colorArr = implode('|||',$colorArr);

                $data_array = array(
                    "user_id" => $user_id,
                    "first_name" => $arr['first_name'],
                    "last_name" => $arr['last_name'],
                    "transaction_id" => $arr["txn_id"],
                    "total_quantity" => $arr["num_cart_items"],
                    "email_addr" => $arr["payer_email"],
                    "address" => $arr["address_street"],
                    "city" => $arr["address_city"],
                    "state" => $arr["address_state"],
                    "country" => $arr["address_country"],
                    "total_amt" => $arr["mc_gross"],
                    "prod_id" => $productArr,
                    "quantity" => $quantityArr,
                    "size" => $sizeArr,
                    "color" => $colorArr,
                    "ip" => $_SERVER['REMOTE_ADDR'],
                    "shipping" => $arr["mc_shipping"],
                    "subtotal" => ceil($arr["mc_shipping"]),
                    //"cart_products" => json_encode($cart_products),
                    "payment_notify" => json_encode($_POST),
                );
                
                $model->insertRecordData($data_array, RKN_ORDER);
                $order_id = $model->getLastInsertId(RKN_ORDER);

                //$max_invoice_number = $model->getTotalCount("rkn_invoice_id", RKN_INVOICE);
                $invoice_data_Array = array(
                    "order_number" => $order_id,
                    "payment_id" => $order_id,
                    "user_id" => $user_id,
                );
                
                $model->insertRecordData($invoice_data_Array, RKN_INVOICE);

                $productArray = array();
                foreach ($cart_products as $cKey => $cValue)
                {
                    $productArray[] = array(
                        "product_name" => $cValue["prod_name"],
                        "product_quantity" => $cValue["product_quantity"],
                        "price" => $cValue["price"],
                    );
                }

                $detail_array = array(
                    "order_number" => $order_number,
                    "user_full_name" => $arr["first_name"] . " " . $arr["last_name"],
                    "gross_amount" => $arr["payment_gross"],
                    "shipping_charge" => $arr["mc_shipping"],
                    "tax" => TAX_PERCENT . "%",
                    "productArray" => $productArray,
                );

                if ($_SERVER['REMOTE_ADDR'] != '127.0.0.1')
                {
                    $email_content = $this->getRecieptEmail($detail_array);
                    $from_email = SITE_EMAIL;
                    $email_model->sendMail($this->user["email_address"], $from_email, SITE_NAME, "Payment Authorization for order - " . $order_number, $email_content);
                }

                $model->deleteRecord("user_id=$user_id", RKN_CART);

                $this->_helper->FlashMessenger(
                        array('success' => '<strong>Success!</strong> Payment success.')
                );
            }
            $this->_redirect(APPLICATION_URL);
        }

        public function paymentcancelAction()
        {
            $this->_helper->FlashMessenger(
                    array('error' => '<strong>Sorry!</strong> Payment not authorized.')
            );
            $this->_redirect(APPLICATION_URL . '/cart');
        }

        function shippingAction()
        {
            $arr = $this->getRequest()->getPost();
            //====== Service codes for ups =====
//            EP - Express Plus or Worldwide Express Plus
//            ES - Express or Worldwide Express
//            1DP - Express Saver or Worldwide Saver
//            EX - Expedited or Worldwide Expedited
//            ST - Standard
//            3DS - 3 Day Select to the United States             
//            ND - UPS Express (NA1)
            $price = $this->getShippingCharge('ST', $arr['shipping_zip']);
            return $price;
        }

        public function getShippingCharge($shipping_code, $zipCode = false)
        {
            $shipping_plugin = new Application_Plugin_ShippingCalculator();
            $shipping = $shipping_plugin->calculate_ups($shipping_code);
            return $shipping;
        }

        public function getRecieptEmail($detail_array)
        {
            $order_number = $detail_array["order_number"];
            $user_full_name = $detail_array["user_full_name"];
            $gross_amount = $detail_array["gross_amount"];
            $shipping_charge = $detail_array["shipping_charge"];
            $tax = $detail_array["tax"];
            $productArray = $detail_array["productArray"];
            $payment_method = "PayPal";

            $str = '
<div align="center">
    <table width="640" cellpadding="0" border="1" style="width:480.0pt;border:solid #cccccc 1.0pt">
        <tbody>
            <tr>
                <td style="border:none;padding:6.0pt 6.0pt .75pt 6.0pt">
                    <p style="margin-bottom:12.0pt" class="MsoNormal">
                        <a target="_blank" href="' . APPLICATION_URL . '">
                            <span style="text-decoration:none">
                                <img width="195" border="0" alt="logo" src="' . IMAGES_PATH . '/logo.png">
                            </span>
                        </a>
                    </p>
                </td>
            </tr>
            <tr>
                <td style="border:none;padding:6.0pt 6.0pt .75pt 6.0pt">
                    <div align="center">
                        <table width="600" cellpadding="0" border="1" style="width:6.25in;border:solid #cccccc 1.0pt">
                            <tbody>
                                <tr>
                                    <td style="border:none;padding:.75pt .75pt .75pt .75pt">
                                        <table width="700" cellpadding="0" border="0" style="width:525.0pt">
                                            <tbody>
                                                <tr>
                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt">
                                                        <p class="MsoNormal"><b><span style="font-size:9.0pt;font-family:Arial;">Hi ' . $user_full_name . ' </span></b><span style="font-size:9.0pt;font-family:Arial">,<br><br>Thank you for shopping on <a target="_blank" href="' . APPLICATION_URL . '">' . SITE_NAME . '.</a> <br><br>This email confirms that ' . SITE_NAME . ' has successfully received your payment of Rs. ' . $gross_amount . ' for your order. <br>Summary of your Order ID : ' . $order_number . ' <u></u><u></u></span></p>
                                                        <table width="100%" cellspacing="0" cellpadding="0" border="0" style="width:100.0%">
                                                            <tbody>
                                                                <tr>
                                                                    <td width="122" style="width:91.5pt;padding:3.0pt 3.0pt 3.0pt 3.0pt">
                                                                        <table width="100%" cellspacing="0" cellpadding="0" border="1" style="width:100.0%;border-collapse:collapse">
                                                                            <tbody>
                                                                                <tr>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt">
                                                                                        <p class="MsoNormal">
                                                                                            <strong>
                                                                                                <span style="font-size:9.0pt">S.No.</span>
                                                                                            </strong>
                                                                                            <span style="font-size:9.0pt"><u></u><u></u></span>
                                                                                        </p>
                                                                                    </td>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt">
                                                                                        <p class="MsoNormal">
                                                                                            <strong>
                                                                                                <span style="font-size:9.0pt">Product Name</span>
                                                                                            </strong>
                                                                                            <span style="font-size:9.0pt"><u></u><u></u></span>
                                                                                        </p>
                                                                                    </td>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt">
                                                                                        <p class="MsoNormal">
                                                                                            <strong>
                                                                                                <span style="font-size:9.0pt">Qty</span>
                                                                                            </strong>
                                                                                            <span style="font-size:9.0pt"><u></u><u></u></span>
                                                                                        </p>
                                                                                    </td>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt">
                                                                                        <p class="MsoNormal">
                                                                                            <strong>
                                                                                                <span style="font-size:9.0pt">Price</span>
                                                                                            </strong>
                                                                                            <span style="font-size:9.0pt"><u></u><u></u></span>
                                                                                        </p>
                                                                                    </td>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt">
                                                                                        <p class="MsoNormal">
                                                                                            <strong>
                                                                                                <span style="font-size:9.0pt">Subtotal</span>
                                                                                            </strong>
                                                                                            <span style="font-size:9.0pt"><u></u><u></u></span>
                                                                                        </p>
                                                                                    </td>
                                                                                </tr>';

            $i = 1;
            foreach ($productArray as $pKey => $pValue)
            {
                $str .= '<tr>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt">
                                                                                        <p class="MsoNormal">
                                                                                            <span style="font-size:9.0pt">' . $i . '<u></u><u></u></span>
                                                                                        </p>
                                                                                    </td>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt">
                                                                                        <p class="MsoNormal">
                                                                                            <span style="font-size:9.0pt">' . $pValue["product_name"] . '<u></u><u></u></span>
                                                                                        </p>
                                                                                    </td>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt">
                                                                                        <p align="right" style="text-align:right" class="MsoNormal"><span style="font-size:9.0pt">' . $pValue["product_quantity"] . '<u></u><u></u></span></p>
                                                                                    </td>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt">
                                                                                        <p align="right" style="text-align:right" class="MsoNormal">
                                                                                            <span style="font-size:9.0pt">' . $pValue["price"] . '<u></u><u></u></span>
                                                                                        </p>
                                                                                    </td>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt"><p align="right" style="text-align:right" class="MsoNormal"><span style="font-size:9.0pt">' . round(($pValue["product_quantity"] * $pValue["price"]), 2) . '<u></u><u></u></span></p></td>
                                                                                </tr>';
                $i++;
            }


            $str .= '<tr>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt" colspan="6"><p class="MsoNormal"><strong><span style="font-size:9.0pt">Sub Total</span></strong><span style="font-size:9.0pt"> (in USD)<u></u><u></u></span></p></td>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt" colspan="2"><p align="right" style="text-align:right" class="MsoNormal"><span style="font-size:9.0pt">' . $gross_amount - $shipping_charge . '<u></u><u></u></span></p></td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt" colspan="6"><p class="MsoNormal"><strong><span style="font-size:9.0pt">Tax</span></strong><span style="font-size:9.0pt"> (in USD)<u></u><u></u></span></p></td>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt" colspan="2"><p align="right" style="text-align:right" class="MsoNormal"><span style="font-size:9.0pt">' . $tax . '<u></u><u></u></span></p></td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt" colspan="6"><p class="MsoNormal"><strong><span style="font-size:9.0pt">Shipping Charge</span></strong><span style="font-size:9.0pt"> (in USD)<u></u><u></u></span></p></td>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt" colspan="2"><p align="right" style="text-align:right" class="MsoNormal"><span style="font-size:9.0pt">' . $shipping_charge . '<u></u><u></u></span></p></td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt" colspan="6"><p class="MsoNormal"><strong><span style="font-size:9.0pt">Net Order value (Amount Paid)</span></strong><span style="font-size:9.0pt"> (in USD)<u></u><u></u></span></p></td>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt" colspan="2"><p align="right" style="text-align:right" class="MsoNormal"><span style="font-size:9.0pt">' . round($gross_amount, 2) . '<u></u><u></u></span></p></td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt" colspan="6"><p class="MsoNormal"><strong><span style="font-size:9.0pt">Payment Type</span></strong><span style="font-size:9.0pt"><u></u><u></u></span></p></td>
                                                                                    <td style="padding:3.75pt 3.75pt 3.75pt 3.75pt" colspan="2"><p align="right" style="text-align:right" class="MsoNormal"><span style="font-size:9.0pt">' . $payment_method . '<u></u><u></u></span></p></td>
                                                                                </tr>
                                                                            </tbody>
                                                                        </table>
                                                                    </td>
                                                                </tr>
                                                            </tbody>
                                                        </table>
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</div>                
';
            return $str;
        }

    }